package com.ssm.controller;

import com.ssm.entity.SysUser;
import com.ssm.service.ElcBlacklistService;
import com.ssm.service.SysUserService;
import com.ssm.service.impl.SysUserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Random;

@Controller
public class IndexController {

    @Resource
    private ElcBlacklistService elcBlacklistService;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("index")
    public String index(){
        return "index";
    }






    @RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST})
    public String index(HttpServletRequest request, HttpServletResponse response) throws IOException {
//
//        //        Random random=new Random();
////        int arr1=random.nextInt(100);
//        int arr1=(int)(Math.random()*100);
//        System.out.println(arr1);
////        int arr2=random.nextInt(100);
//        int arr2=(int)(Math.random()*100);
//        System.out.println(arr2);
//        int arr3=arr1+arr2;
//        request.setAttribute("arr1",arr1);
//        request.setAttribute("arr2",arr2);

        String userCode=request.getParameter("loginId");
        String password=request.getParameter("password");
        String userYan=  request.getParameter("userCode");
//        int userYan1=Integer.parseInt(userYan);
        SysUser sysUser = new SysUser();
        sysUser.setUserCode(userCode);
        //调用service方法判断用户是否登陆成功
        HttpSession session=request.getSession();
        String checkCode=(String) session.getAttribute("checkCode");
        if(userCode.length()==0 || password.length()==0 || userYan.length()==0){
            request.getSession().setAttribute("x",4);
            return "redirect:index";
        }
         else if (!checkCode.equalsIgnoreCase(userYan) ){
            request.getSession().setAttribute("x", 1);
            return "redirect:index";

        }

        //根据账号去数据库查是否有这个对象
        List<SysUser> users =sysUserService.queryAll(sysUser);



        if (users==null   || users.size()==0){

            request.getSession().setAttribute("x", 2);
            return "redirect:index";
        }

        else {
            SysUser sysUser1 = users.get(0);
            if (Objects.equals(sysUser1.getUserPwd(),password)){
                //对比密码后如果密码正确则登陆成功，并获取到session对象
                if (sysUser1.getLocked()==0){
                    session.setAttribute("sysUser",sysUser1);
                    return "redirect:showMain";
                }else {
                    request.getSession().setAttribute("x", 5);
                    return "redirect:index";
                }

            }else {
                request.getSession().setAttribute("x", 3);
                return "redirect:index";
            }
        }

    }


    @RequestMapping("elcBlack/index1")
    public String elcBlackIndex(){
        return "elcBlack/elcBlack";
    }




    @RequestMapping("404")
    public String four(){
        return "404";
    }



    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:index";
    }



}
